package com.yuexun.dhdoctor.module.personalPlan.ui.fragment;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.personalPlan.adapter.OtherPlanAdapter;
import com.yuexun.dhdoctor.module.personalPlan.bean.LockTimeBean;
import com.yuexun.dhdoctor.module.personalPlan.bean.OtherPlanBean;
import com.yuexun.dhdoctor.module.personalPlan.model.impl.PlanMainModel;
import com.yuexun.dhdoctor.module.personalPlan.presenter.impl.PlanMainPresenter;
import com.yuexun.dhdoctor.module.personalPlan.ui.Act_OtherPlan;
import com.yuexun.dhdoctor.module.personalPlan.ui.view.IPlanMainView;
import com.yuexun.dhdoctor.widget.EmptyLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/4/20 0020.
 */

public class Frag_OtherPlan extends KJFragment implements IPlanMainView {

    @Bind(R.id.task_share_list)
    PullToRefreshList taskShareList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    int pageSize = 9999;
    int pageIndex = 1;
    PlanMainPresenter presenter;
    private ListView mList;
    ArrayList<OtherPlanBean.DatasEntity> datasEntities = new ArrayList<>();
    BaseAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_other_plan, null);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        return null;
    }


    private void init() {
        presenter = new PlanMainPresenter(new PlanMainModel(), this);
        listViewPreference();

        set_Other();
    }

    private void set_Other() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("pageIndex", pageIndex + "");
        params.put("pageSize", pageSize + "");
        presenter.getOtherTask(params);
    }

    private void listViewPreference() {
        mList = taskShareList.getRefreshView();
        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        taskShareList.setPullLoadEnabled(false);//设置上拉刷新功能关闭
        ((FooterLoadingLayout) taskShareList.getFooterLoadingLayout()).setNoMoreDataText("已经是最新状态");

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                Intent intent = new Intent(getActivity(), Act_OtherPlan.class);
                bundle.putString("shareId", datasEntities.get(position).getAccountId());
                bundle.putString("name", datasEntities.get(position).getFullName());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        taskShareList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                Map<String, String> params = new HashMap<>();
                params.put("sessionUuid", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
                params.put("pageIndex", pageIndex + "");
                params.put("pageSize", pageSize + "");
                presenter.getOtherTask(params);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                taskShareList.setHasMoreData(false);
            }
        });

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);

            }
        });
    }

    @Override
    public void showTask(String s) {

    }

    @Override
    public void showOtherTask(ArrayList<OtherPlanBean.DatasEntity> datasEntity) {
        if (datasEntity != null && datasEntity.size() > 0) {
            datasEntities = datasEntity;
            adapter = new OtherPlanAdapter(mList, datasEntities, R.layout.item_task_share);
            mList.setAdapter(adapter);
        }
        emptyLayout.dismiss();
    }

    @Override
    public void PullDownRefreshState(int state) {
        switch (state) {
            case AppConfig.DOWNLOAD_SUCCESS:
                taskShareList.onPullDownRefreshComplete();
                taskShareList.onPullUpRefreshComplete();
                break;
            case AppConfig.DOWNLOAD_NETERROR:
                emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                break;
            case AppConfig.DOWNLOAD_NODATA:
                emptyLayout.setErrorType(EmptyLayout.NODATA);
        }
    }

    @Override
    public void lockTime(LockTimeBean lockTimeBean) {

    }
}
